package com.wh.controller;

import com.alibaba.fastjson.JSONObject;
import com.wh.beans.LoginInfoCookie;
import com.wh.model.WorkProjectTask;
import com.legend.security.Authority;
import com.legend.security.PermissionType;
import com.wh.service.IExt.IWorkProjectTaskService;
import com.wh.service.impl.CommonBo;
import com.wh.service.impl.WorkProjectTaskService;
import com.legend.utils.*;

import org.apache.log4j.Logger;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/*
*author:徐洪昌
*blog:http://cnblogs.com/xusir
*datetime:2014-07-25 12:40:33
*/
@Controller
@RequestMapping(value = "/api/WorkProjectTask")
public class WorkProjectTaskController {
    @InitBinder
    protected void init(HttpServletRequest request, ServletRequestDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(
                dateFormat, false));
    }
    Logger log = Logger.getLogger(WorkProjectTaskController.class);
    private static final String MODULECODE = "WorkProjectTask";

    @Authority(moduleCode = MODULECODE, permissionType = PermissionType.View)
    @RequestMapping(value = "view", method = RequestMethod.GET)
    public ModelAndView view(HttpServletRequest request) {
        String id = request.getParameter("id");
        IWorkProjectTaskService service = WorkProjectTaskService.getInstance();
        ModelAndView mv = new ModelAndView();
        if (id != null) {
            ReturnResult result = service.selectById(id);
            log.debug(JSONObject.toJSONString(result));
            mv.addObject("info", result);
        }
        mv.setViewName("/project/"+MODULECODE+"/view");
        return mv;
    }
//IWorkProjectTaskService service = WorkProjectTaskService.getInstance();

    @Authority(moduleCode = MODULECODE, permissionType = PermissionType.Delete)
    @RequestMapping(value = "del", method = RequestMethod.POST)
    @ResponseBody
    public String delete(HttpServletRequest request) {
        String ids = request.getParameter("id");
        IWorkProjectTaskService service = WorkProjectTaskService.getInstance();
        ReturnResult result = service.delByIds(ids);
        return JSONObject.toJSONString(result);
    }

    @Authority(moduleCode = MODULECODE, permissionType = PermissionType.On)
    @RequestMapping(value = "audit", method = RequestMethod.POST)
    @ResponseBody
    public String audit(HttpServletRequest request) {
        String ids = request.getParameter("ids");
        Integer state = StringUtils.stringToInteger(request.getParameter("state"));
        ReturnResult result = new ReturnResult();
        if (!StringUtils.isEmpty(ids)) {
            String[] idArr = ids.split(",");
            IWorkProjectTaskService service = WorkProjectTaskService.getInstance();
            result = service.audit(state, idArr);
        } else {
            result.setMsg(i18nUtils.getVal("100005000"));
        }
        return JSONObject.toJSONString(result);
    }

    @Authority(moduleCode = MODULECODE, permissionType = PermissionType.Edit)
    @RequestMapping(value = "edit", method = RequestMethod.POST)
    @ResponseBody
    public String edit(@ModelAttribute WorkProjectTask entity) {
        IWorkProjectTaskService service = WorkProjectTaskService.getInstance();
        ReturnResult result = service.editByModel(entity);
        result.setData(entity);
        return JSONObject.toJSONString(result);
    }

    @Authority(moduleCode = MODULECODE, permissionType = PermissionType.Add)
    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public String add(@ModelAttribute WorkProjectTask entity,HttpServletRequest request) {
        IWorkProjectTaskService service = WorkProjectTaskService.getInstance();
        LoginInfoCookie info= CommonBo.getInstance().checkLogin(request);
        entity.setCreateUserId(info.getUserId());
        ReturnResult result = service.insertByModel(entity);
        result.setData(entity);
        return JSONObject.toJSONString(result);
    }

    @Authority(moduleCode = MODULECODE, permissionType = PermissionType.List)
    @RequestMapping("getList")
    @ResponseBody
    public String getList(HttpServletRequest request, @RequestParam(value = "page", required = true, defaultValue = "1") Integer pageIndex,
                          @RequestParam(value = "rows", required = true, defaultValue = "10") Integer pageSize) {
        IWorkProjectTaskService service = WorkProjectTaskService.getInstance();
        Map<String, Object> map = StringUtils.getSearchMap(request);
        ReturnResult result = service.selectByMap(pageIndex, pageSize, map);
        return JSONObject.toJSONString(result);
    }

    @Authority(moduleCode = MODULECODE, permissionType = PermissionType.Sort)
    @RequestMapping(value = "sort/{sorts}/{ids}")
    @ResponseBody
    public String sort(@PathVariable("sorts") String sorts, @PathVariable("ids") String ids) {
        IWorkProjectTaskService service = WorkProjectTaskService.getInstance();
        sorts = Base64.encodereplace(sorts);
        ids = Base64.encodereplace(ids);
        sorts = new String(Base64.decode(sorts.toCharArray()));
        ids = new String(Base64.decode(ids.toCharArray()));
        ReturnResult result = service.sort(sorts, ids);

        return JSONObject.toJSONString(result);
    }

    @RequestMapping(value = "list/{projectId}")
    public ModelAndView list(@PathVariable("projectId") String projectId)
    {
        ModelAndView mv=new ModelAndView();
        mv.addObject("projectId",projectId);
        mv.setViewName("project/WorkProjectTask/list");
        return mv;
    }
    @RequestMapping(value = "form/{projectId}/{index}")
    public ModelAndView form(@PathVariable("index") Integer index,@PathVariable("projectId") String projectId)
    {
        ModelAndView mv=new ModelAndView();
        mv.addObject("index",index);
        mv.addObject("projectId",projectId);
        mv.setViewName("project/WorkProjectTask/form");
        return mv;
    }
}